package cn.inbot.shopadmin.module.robot.viewmodel;

import androidx.lifecycle.MutableLiveData;

import org.jetbrains.annotations.NotNull;

import cn.inbot.shopadmin.app.AppConfig;
import cn.inbot.shopadmin.app.AppGlobalData;
import cn.inbot.shopadmin.bean.LoginResult;
import cn.inbot.shopadmin.bean.RobotVoListResult;
import cn.inbot.shopadmin.module.robot.repository.RobotRepository;
import cn.inbot.lib.framework.PBApplication;
import cn.inbot.lib.framework.mvvm.java.ApiCallbackAdapter;
import cn.inbot.lib.framework.mvvm.java.PBJViewModel;
import cn.inbot.lib.log.PBLog;
import cn.inbot.lib.network.BaseResponse;
import cn.inbot.lib.network.ResponseWrapper;
import cn.inbot.lib.push.GetuiServiceCallback;
import cn.inbot.lib.push.PBPushManager;

/**
 * Created by shisenkun on 2021/5/11.
 */
public class RobotViewModel extends PBJViewModel {
    private final RobotRepository mRepository = new RobotRepository();
    public MutableLiveData<ResponseWrapper<RobotVoListResult>> robotVoListResponseWrapper = new MutableLiveData();

    public void getRobotVoList() {
        if (AppGlobalData.sLoginResult != null) {
            apiCall(
                    mRepository.getRobotVoList(AppGlobalData.sLoginResult.getUsername(), AppGlobalData.sLoginResult.getLoginUuid()),
                    new ApiCallbackAdapter<ResponseWrapper<RobotVoListResult>>() {
                        @Override
                        public void onCompleted(ResponseWrapper<RobotVoListResult> responseWrapper) {
                            super.onCompleted(responseWrapper);
                            robotVoListResponseWrapper.setValue(responseWrapper);
                        }
                    });
        }
    }

    public void startPushService() {
        if (AppGlobalData.sLoginResult != null) {
            startWebSyncService(AppGlobalData.sLoginResult);
            startGeituService(AppGlobalData.sLoginResult);
        }
    }

    private void startWebSyncService(LoginResult loginResult) {
        PBPushManager.Companion.getInstance(PBApplication.appContext)
                .startWebSyncService(loginResult.getUsername(),
                        loginResult.getLoginUuid(),
                        AppConfig.REGISTER_APP,
                        AppConfig.APP_TYPE,
                        AppConfig.WEB_SYNC_SERVER_IP);
    }

    private void startGeituService(LoginResult loginResult) {
        PBLog.INSTANCE.d("启动个推服务");
        PBPushManager.Companion.getInstance(PBApplication.appContext)
                .startGetuiService(new GetuiServiceCallback() {
                    @Override
                    public void onReceivedGetuiClientId(@NotNull String getuiClientId) {
                        PBLog.INSTANCE.d("保存个推id:" + getuiClientId);
                        apiCall(
                                mRepository.saveGeTuiClientId(loginResult.getUserId(), getuiClientId),
                                5,
                                5,
                                new ApiCallbackAdapter<ResponseWrapper<BaseResponse>>() {
                                    @Override
                                    public void onSuccess(ResponseWrapper<BaseResponse> responseWrapper) {
                                        super.onSuccess(responseWrapper);
                                        PBLog.INSTANCE.d("保存个推id成功");
                                    }

                                    @Override
                                    public void onError(ResponseWrapper<BaseResponse> responseWrapper) {
                                        super.onError(responseWrapper);
                                        PBLog.INSTANCE.d("保存个推id失败");
                                    }
                                });
                    }
                });
    }

}
